package com.panopset;

import static com.panopset.Util.isPopulated;

import java.awt.Color;

import javax.swing.JTextField;

import com.panopset.gui.LookAndFeel;

/**
 * User alert mechanism.
 *
 * @author Karl Dinwiddie
 *
 */
public final class Alert {

    /**
     * Alert color.
     */
    private Color alertColor = LookAndFeel.Colors.DSPMSG_GREEN_ALERT
            .getColor();

    /**
     * Red message.
     */
    private String redMsg = "";

    /**
     * Yellow message.
     */
    private String yellowMsg = "";

    /**
     * Green message.
     */
    private String greenMsg = "";

    /**
     * Update.
     */
    private void update() {
        if (isPopulated(redMsg)) {
            alertColor = LookAndFeel.Colors.DSPMSG_RED_ALERT.getColor();
            getMsgLabel().setText(redMsg);
        } else if (isPopulated(yellowMsg)) {
            alertColor = LookAndFeel.Colors.DSPMSG_YELLOW_ALERT
                    .getColor();
            getMsgLabel().setText(yellowMsg);
        } else if (isPopulated(greenMsg)) {
            alertColor = LookAndFeel.Colors.DSPMSG_GREEN_ALERT
                    .getColor();
            getMsgLabel().setText(greenMsg);
        } else {
            getMsgLabel().setText("");
        }
        if (AbstractApplication.getApp() != null) {
            AbstractApplication.getApp().getMenuBar().repaint();
        }
    }

    /**
     * Clear all messages.
     */
    public static void clearAll() {
        getInstance().redMsg = "";
        getInstance().yellowMsg = "";
        getInstance().greenMsg = "";
        getInstance().update();
    }

    /**
     * Clear red message.
     */
    public static void clearRed() {
        getInstance().redMsg = "";
        getInstance().update();
    }

    /**
     * Clear yellow message.
     */
    public static void clearYellow() {
        getInstance().yellowMsg = "";
        getInstance().update();
    }

    /**
     * @return Alert color.
     */
    public Color getAlertColor() {
        return alertColor;
    }

    /**
     * Red alert.
     * @param msg Message.
     */
    public static void red(final String msg) {
        System.out.println(msg);
        getInstance().redMsg = msg;
        getInstance().update();
    }

    /**
     * Yellow alert.
     * @param msg Message.
     */
    public static void yellow(final String msg) {
        System.out.println(msg);
        getInstance().yellowMsg = msg;
        getInstance().update();
    }

    /**
     * Green alert.
     * @param msg Message.
     */
    public static void green(final String msg) {
        System.out.println(msg);
        getInstance().greenMsg = msg;
        getInstance().update();
    }

    /**
     * Clear the top message.
     */
    public static void clearTopMessage() {
        getInstance().clearTopMessage00();
    }

    /**
     * Clear the top message.
     */
    private void clearTopMessage00() {
        if (isPopulated(redMsg)) {
            red("");
        } else if (isPopulated(yellowMsg)) {
            yellow("");
        } else if (isPopulated(greenMsg)) {
            green("");
        }
    }

    /**
     * @return Message.
     */
    public static String getMessage() {
        return getInstance().getMsgLabel().getText();
    }

    /**
     * @return Alert color.
     */
    public static Color getColor() {
        return getInstance().getAlertColor();
    }

    /**
     * Message label.
     */
    private JTextField msgLabel;

    /**
     * @return Message label.
     */
    public JTextField getMsgLabel() {
        if (msgLabel == null) {
            msgLabel = new JTextField();
            msgLabel.setFont(LookAndFeel.Fonts.TITLE_FONT_NORMAL.getFont());
            msgLabel.setEditable(false);
        }
        return msgLabel;
    }


    /**
     * Based on SingletonHolder inner class by Bill Pugh.
     *
     * <h5>References</h5>
     * <ul>
     * <li>
     * <a href="http://en.wikipedia.org/wiki/Singleton_pattern">
     * http://en.wikipedia.org/wiki/Singleton_pattern </a></li>
     * </ul>
     *
     */
    private static final class Singleton {
        /**
         * Instance variable.
         */
        private static final Alert INSTANCE = new Alert();

        /**
         * Private constructor.
         */
        private Singleton() {
            // Prevent instantiation.
        }
    }

    /**
     * @return static FlagIcons instance.
     */
    protected static Alert getInstance() {
        return Singleton.INSTANCE;
    }

    /**
     * Private singleton constructor.
     */
    private Alert() {
    }

}
